import requests
import json
from comm.module import *
# from comm.LogUtils import logger
# from comm.postgresutility import postsql_utility
import datetime
import hashlib

#6. 学生 新增或修改
def doAddStudent():
    try:

        timestamp = int((datetime.datetime.now().timestamp()) * 1000)
        # print("时间："+timestamp)

        # sign 签名 生成方式:md5(时间戳+appId+秘钥)
        temp_data = str(timestamp) + str(brand_appid) + str(brand_appkey)
        #print(temp_data)
        md5 = hashlib.md5()  # 创建一个md5对象
        md5.update(temp_data.encode('utf-8'))  # 使用utf-8编码数据
        temp_sign = md5.hexdigest()  # 返回加密后的十六进制字符串
        #print("sign:" + temp_sign)

        parentList = []
        icCardList=[]
        faceList = []

        payload = json.dumps({
            "timestamp": timestamp,
            "appId": brand_appid,
            "sign": temp_sign,
            "dataList": [{
                "studentUniqueCode": "2024501",
                "code":"X2024501",
                "name": "曹植",
                "sex": 1,
                "clazzUniqueCode":"9001",
                "parentList":parentList,
                "icCardList": icCardList,
                "faceList": faceList
            },{
                "studentUniqueCode": "2024502",
                "code":"X2024502",
                "name": "曹丕",
                "sex": 1,
                "clazzUniqueCode":"9001",
                "parentList":parentList,
                "icCardList": icCardList,
                "faceList": faceList
            }]
        })

        headers = {
            'Content-Type': 'application/json'
        }
        response = requests.request("POST", brand_url + "/student/v1", headers=headers, data=payload)
        # res = response.text.encode('utf8')
        data = response.json()
        # data = data.decode("utf-8")
        print(data)
        # logger.info('141:' + data.decode("utf-8"))
        response.close()

        # json_str = json.loads(data)
        # ierrCode = json_str["errCode"]
    except Exception as e:
        print(str(e))


#
doAddStudent()